import re
from concurrent.futures.thread import ThreadPoolExecutor

import requests

pattern = re.compile('<title>(?P<foo>.*?)</title>')


def fetch_page(url):
    print('Fetch:', url)
    resp = requests.get(url, headers={
        'User-Agent': 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_14_6) '
                      'AppleWebKit/537.36 (KHTML, like Gecko) '
                      'Chrome/83.0.4103.97 Safari/537.36',
        'Accept': 'text/html,application/xhtml+xml,application/xml;'
                  'q=0.9,image/webp,image/apng,*/*;'
                  'q=0.8,application/signed-exchange;v=b3;q=0.9',
        'Accept-Language': 'zh-CN,zh;q=0.9,en;q=0.8',
    })
    print(f'{url}: {resp.status_code}')
    match = pattern.search(resp.text)
    print(match.group('foo'))


def main():
    urls = [
        'https://www.douban.com/',
        'http://www.sohu.com/',
        'http://www.sina.com.cn/',
        'https://www.taobao.com/',
        'http://jd.com/',
        'https://www.python.org/',
        'https://github.com/',
        'https://www.linkedin.com/',
        'https://leetcode.com/',
        'https://pypi.org/',
    ]
    with ThreadPoolExecutor(max_workers=8) as pool:
        for url in urls:
            pool.submit(fetch_page, url)
    # for url in urls:
    #     fetch_page(url)


if __name__ == '__main__':
    main()
